﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace GoPlay.Models
{
    public class EFTeamRepository : ITeamRepository
    {

        //private GoPlay.Models.Team.TeamsContext _db = new GoPlay.Models.Team.TeamsContext();
        private GoPlayDbContext _db = new GoPlayDbContext();

        public Team GetTeamByID(int id)
        {
            return _db.Teams.FirstOrDefault(d => d.Id == id);
        }

        public IEnumerable<Team> GetAllTeamsForUser(int userId)
        {
            return _db.Teams.Where(x => x.UserProfile.UserId == userId).ToList();
        }

        public int CreateNewTeam(Team team)
        {
            _db.Teams.Add(team);
            _db.SaveChanges();
            return team.Id;
        }

        public int SaveChanges()
        {
            return _db.SaveChanges();
        }

        public void DeleteTeam(int id)
        {
            var t = GetTeamByID(id);
            _db.Teams.Remove(t);
            _db.SaveChanges();
        }

        public void UpdateTeam(Team team)
        {
            _db.Entry(team).State = EntityState.Modified;
            _db.SaveChanges();
        }

    }
}